package com.ruoyi.app.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.app.annotation.AppSecurity;
import com.ruoyi.app.util.ReqMsg;
import com.ruoyi.app.util.RspMsg;
import com.ruoyi.business.domain.WalletRepayReq;
import com.ruoyi.business.domain.WalletWithdrawReq;
import com.ruoyi.business.service.ILoanService;
import com.ruoyi.business.service.IWalletService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.RspEnmu;
import com.ruoyi.common.exception.BusinessException;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * @Author: jamnsin
 * @Date: 2022/6/10 11:24
 */
@RestController
@RequestMapping("/app/wallet")
public class WalletController extends BaseController {

    @Autowired
    IWalletService walletService;

    @Autowired
    ILoanService loanService;

    @AppSecurity
    @ApiOperation("获取客户钱包信息")
    @PostMapping("/account")
    @Log(title = "获取客户钱包信息", businessType = BusinessType.GET)
    public RspMsg getWalletAccount(@RequestBody @Validated ReqMsg reqMsg) {
        return RspMsg.RspMsgOk(walletService.getWalletAccount(this.getCustNo()));
    }

    @AppSecurity
    @ApiOperation("获取钱包收入明细")
    @PostMapping("/income")
    @Log(title = "获取钱包收入明细", businessType = BusinessType.GET)
    public RspMsg getWalletIncome(@RequestBody @Validated ReqMsg reqMsg) {
        return RspMsg.RspMsgOk(walletService.getWalletIncome());
    }

    @AppSecurity
    @ApiOperation("获取钱包支出明细")
    @PostMapping("/expend")
    @Log(title = "获取钱包支出明细", businessType = BusinessType.GET)
    public RspMsg getWalletExpend(@RequestBody @Validated ReqMsg reqMsg) {
        return RspMsg.RspMsgOk(walletService.getWalletExpend());
    }

    @AppSecurity
    @ApiOperation("钱包提现")
    @PostMapping("/withdraw")
    @Log(title = "钱包提现", businessType = BusinessType.SUBMIT)
    public RspMsg walletWithdraw(@RequestBody @Validated ReqMsg reqMsg) {

        String userId = this.getUserId();
        appService.repeatSubmit(userId);

        WalletWithdrawReq walletWithdrawReq = JSON.parseObject(JSON.toJSONString(reqMsg.getData()), WalletWithdrawReq.class);

        // 校验参数
        validReqParam(walletWithdrawReq);

        // 校验密码
        checkTransPwd(userId, walletWithdrawReq.getTransPwd());

        loanService.checkWalletWithdrawIouStatus();

        walletService.walletWithdraw(walletWithdrawReq);

        return RspMsg.RspMsgOk();
    }

    @AppSecurity
    @PostMapping("/repayment")
    @Log(title = "钱包还款", businessType = BusinessType.SUBMIT)
    public RspMsg walletRepay(@RequestBody @Validated ReqMsg reqMsg) {

        WalletRepayReq walletRepayReq = JSON.parseObject(JSON.toJSONString(reqMsg.getData()), WalletRepayReq.class);
        walletService.walletRepayment(walletRepayReq);

        return RspMsg.RspMsgOk();
    }

    private void validReqParam(WalletWithdrawReq walletWithdrawReq) {
        checkAttribute(walletWithdrawReq);
        if (walletWithdrawReq.getApplyAmount().compareTo(BigDecimal.ZERO) == 0) {
            throw new BusinessException(RspEnmu.WALLET_WITHDRAW_ZERO);
        }
    }

}
